/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package url;

import java.net.URL;

/**
 *
 * @author kuksenok
 */
public class Info {


    /**
     * Uses the ENCODING list to validate an http string.
     * @param in String representation of a URL
     * @return String representation of a URL which has all the "illegal" characters replaced with corresponding encodings
     */
    public static String encodeURLSource(String in) {
        String out = "";
        boolean doEncode;
        for (int i = 0; i < in.length(); i++) {
            doEncode = false;
            for (int j = 0; j < Info.ENCODING.length && !doEncode; j += 2) {

                if (in.charAt(i) == Info.ENCODING[j].charAt(0)) {
                    out += "%" + Info.ENCODING[j + 1];
                    doEncode = true;
                }
            }
            if (!doEncode) {
                out += in.charAt(i);
            }
        }
        return out;
    }

    public static final String[] GOOD_EXT = new String[]{"htm", "html",
        "htmls", "php", "php4", "php5", "phtm", "phtml", "sht",
        "shtm", "shtml", "stm", "stml", "xhtm", "xhtml", "xhtml", "txt", "asp"};
    public static final String[] BAD_EXT = new String[]{"pdf", "wmv", "doc", "docx",
        "swf", "jpg", "jpeg", "mp4", "mp3", "avi"};
    /**
     * List of encoding pairs; described below.
     */
    public static final String[] ENCODING = new String[]{" ", "20"};

    /**
     * A list of exensions that contain legal, non-directory content. We assume,
     * which is mostly reasonable, that any address that has this extension
     * is not a directory. (We do not assume the converse of that, though)

    public static String[] getGoodExtensions() {
    return GOOD_EXT;
    }

    public static void addGoodExtension(String ext) {
    String[] nGE = copyStrArr(GOOD_EXT, 1);
    nGE[GOOD_EXT.length] = ext;
    GOOD_EXT = nGE;
    }

    public static void clearGoodExtension() {
    GOOD_EXT = new String[0];
    }
    public static String[] getBadExtensions() {
    return BAD_EXT;
    }

    /**
     * A list of extensions to give up on. The package in questions only reads online content,
     * not the .pdf files and other documents. These are extensions we simply "give up" on
     * when we see them.

    public static void addBadExtension(String ext) {
    String[] nBE = copyStrArr(BAD_EXT, 1);
    nBE[BAD_EXT.length] = ext;
    BAD_EXT = nBE;
    }

    public static void clearBadExtension() {
    BAD_EXT = new String[0];
    }

    /**
     * Helper for setters for the string arrays above.
     * @param orig
     * @param add
     * @return

    private static String[] copyStrArr(String[] orig, int add) {
    String[] n = new String[orig.length + add];
    for (int i = 0; i < orig.length; i++) {
    n[i] = orig[i];
    }
    return n;
    }



    /**
     * Getter for the list of url encoding pairs, s.t. even-indexed (2n) elements are characters forbidden
     * in the HTTP protocol, and the adjacent odd-indexed elements (2n+1) are the corresponding
     * hex encodings. Used to replace every occurence of an illegal
     * character at 2n with a string formed by concatenating a "%" to the front of the (2n+1)
     * encoding.

    public static String[] getURLEncodings() {
    return ENCODING;
    }

    /**
     * Adds an encoding pair
     * @param HTTP: string that could appear in HTTP address, but wihtou the % sign (eg, HTTP space would be "20" here)
     * @param translation: what this would be interpresed as (eg, for the above=20, this would be=" ")

    public static void addURLEncodings(String HTTP, String translation) {
    String[] nENC = copyStrArr(ENCODING, 2);
    nENC[ENCODING.length] = translation;
    nENC[ENCODING.length + 1] = HTTP;
    ENCODING = nENC;
    }

    /**
     * Delected all HTTP address encoding pairs.

    public static void clearURLEncodings() {
    ENCODING = new String[0];
    }
     */
    public static String makeDirectory(URL u) {
        if (u == null) {
            return null;
        }
        String path = u.getPath();
        String host = u.getHost();
        String query = u.getQuery();
        String protocol = u.getProtocol();
        int port = u.getPort();
        return makeDirectory(protocol, host, port, path, query);
    }

    public static String makeDirectory(String protocol, String host, int port, String path, String query) {
        path = noDirPath(path, true);
        host = noDirPath(host, true);
        StringBuilder source = new StringBuilder(protocol);
        source.append("://").append(host);
        if (port != -1) {
            source.append(':').append(port);
        }
        if (path.length() > 0) {
            source.append('/').append(port);
        }
        source.append(':');
        if (query != null) {
            source.append('?').append(port);
        }
        return source.toString();
    }

    public static String noPath(String protocol, String host, int port) {
        host = noDirPath(host);
        StringBuilder source = new StringBuilder(protocol);
        source.append("://").append(host);
        if (port != -1) {
            source.append(":").append(port);
        }
        return source.toString();
    }

    public static String noDirPath(String path) {
        return noDirPath(path, false);
    }

    public static String noDirPath(String path, boolean stripfront) {
        int k = path.length()-1, cutend=0, cutfront=0;
        while(k>=0){
            if(path.charAt(k)!='/'){
                break;
            }
            cutend++;
        }
        while(k<path.length()){
            if(path.charAt(k)!='/'){
                break;
            }
            cutfront++;
        }
        System.out.println("Stripped path: " + path.substring(cutfront, path.length()-cutend));
        return path;
    }
}
